package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.Agent;
import com.ruoyi.system.service.IAgentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AgentRelationship;
import com.ruoyi.system.service.IAgentRelationshipService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 中介联系记录Controller
 * 
 * @author ruoyi
 * @date 2021-03-14
 */
@Controller
@RequestMapping("/system/relationship")
public class AgentRelationshipController extends BaseController
{
    private String prefix = "system/relationship";

    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentRelationshipService agentRelationshipService;

    @RequiresPermissions("system:relationship:view")
    @GetMapping()
    public String relationship()
    {
        return prefix + "/relationship";
    }

    /**
     * 查询中介联系记录列表
     */
    @RequiresPermissions("system:relationship:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgentRelationship agentRelationship)
    {
        startPage();
        List<AgentRelationship> list = agentRelationshipService.selectAgentRelationshipList(agentRelationship);
        return getDataTable(list);
    }

    /**
     * 导出中介联系记录列表
     */
    @RequiresPermissions("system:relationship:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgentRelationship agentRelationship)
    {
        List<AgentRelationship> list = agentRelationshipService.selectAgentRelationshipList(agentRelationship);
        ExcelUtil<AgentRelationship> util = new ExcelUtil<AgentRelationship>(AgentRelationship.class);
        return util.exportExcel(list, "relationship");
    }

    /**
     * 新增中介联系记录
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        Agent agent = agentService.selectAgentById(id);
        mmap.put("agent", agent);
        return prefix + "/add";
    }

    /**
     * 新增保存中介联系记录
     */
    @RequiresPermissions("system:relationship:add")
    @Log(title = "中介联系记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AgentRelationship agentRelationship)
    {
        Agent agent = agentService.selectAgentById(agentRelationship.getAgentId());
        agent.setRelationTime(DateUtils.getNowDate());
        agentService.updateAgent(agent);
        agentRelationship.setUserId(ShiroUtils.getUserId());
        agentRelationship.setDeptId(ShiroUtils.getSysUser().getDeptId());
        return toAjax(agentRelationshipService.insertAgentRelationship(agentRelationship));
    }

    /**
     * 修改中介联系记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AgentRelationship agentRelationship = agentRelationshipService.selectAgentRelationshipById(id);
        mmap.put("relationship", agentRelationship);
        return prefix + "/edit";
    }

    /**
     * 修改保存中介联系记录
     */
    @RequiresPermissions("system:relationship:edit")
    @Log(title = "中介联系记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgentRelationship agentRelationship)
    {
        return toAjax(agentRelationshipService.updateAgentRelationship(agentRelationship));
    }

    /**
     * 删除中介联系记录
     */
    @RequiresPermissions("system:relationship:remove")
    @Log(title = "中介联系记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(agentRelationshipService.deleteAgentRelationshipByIds(ids));
    }
}
